package com.suxin.handler;

import com.suxin.annotation.CurrentUser;
import com.suxin.domain.User;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Parameter;
import java.time.Clock;

/**
 * @author Tang
 * @classname MyHandlerMethodArgumentResolver
 * @description [  ]
 * @date 2021/3/11 16:43
 */
public class MyHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        Parameter parameter1 = parameter.getParameter();
        return parameter1.getType() == User.class &&
                parameter1.isAnnotationPresent(CurrentUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        System.out.println("------方法参数解析器执行了-------");
        HttpServletRequest request = (HttpServletRequest)webRequest.getNativeRequest();
        User user = new User();
        user.setName(request.getParameter(Clock.systemDefaultZone().millis() + ""));
        return user;
    }
}